package web_hr;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.deploy.SessionConfig;

/**
 * Servlet implementation class CheckUpdate
 */
@WebServlet("/CheckUpdate")
public class CheckUpdate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckUpdate() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pass1 =request.getParameter("password1");
		String pass2 =request.getParameter("password2");
		String pass =request.getParameter("password");
		String oldpass = null;
		PrintWriter out =response.getWriter();
		String  username=(String) request.getSession().getAttribute("username");
		Model m=new Model("hr_employee");
		try{
			ResultSet res=m.eQuery("select  password  from  hr_employee  where username='"+username+"'");
			if (res!=null){
				res.next();
				oldpass=res.getString("password");	
			}
		}catch (SQLException e)
		{
			e.printStackTrace();
		}
		//int flage=1;
		
		if (!(oldpass.equals(pass)))
		{
			 request.setAttribute("message","Old Password is not crrect ");
				request.setAttribute("url","employee/updateEmployee.jsp");
				request.getRequestDispatcher("/redirect.jsp").forward(request,response);
		}
		else{
			if (pass1.equals(pass2)){
				ResultSet rs=m.eQuery("update   hr_employee  set   password ='"+pass1+"' where username='"+username+"'");   
				try{
					if (rs!=null) {
						request.setAttribute("message","UpdateSuccessful");
						request.setAttribute("url","employee/updateEmployee.jsp");
						request.getRequestDispatcher("/redirect.jsp").forward(request,response);
					}
				}catch (Exception e){
					e.printStackTrace();
				}finally{
					 m.close();
				}
			}
			else {
				request.setAttribute("message","UpdateSuccessful");
				request.setAttribute("url","employee/updateEmployee.jsp");
				request.getRequestDispatcher("/redirect.jsp").forward(request,response);
			}
			//select user is exit or not
		}
	}

}
